package com.fancymusic.controller;

import com.fancymusic.entity.Album;
import com.fancymusic.entity.BaseResult;
import com.fancymusic.service.IAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("album")
public class AlbumController {


    @Autowired
    IAlbumService   iAlbumService;

    /**
     * 根据传入专辑名称查找专辑
     * @param album
     * @return
     */
    @RequestMapping("getAlbumByName")
    public BaseResult<Album> getSingleByName(@RequestBody Album album) {

        BaseResult<Album> result = new BaseResult<>();

        if (album == null){
            result.setMessage("专辑信息输入错误");
            return result;
        }


        List<Album> albumByName = iAlbumService.findAlbumByName(album);

        if (albumByName == null){
            result.setMessage("专辑不存在");
            return result;
        }

        result.setList(albumByName);
        result.setMessage("专辑信息已找到");

        return result;
    }


    /**
     * 保存专辑
     */
    @RequestMapping("save")
    public BaseResult<Album> save(@RequestBody Album album){

        System.out.println("AlbumController===save===album==="+album);
        BaseResult<Album> albumBaseResult = new BaseResult<>();

        iAlbumService.saveAlbum(album);
        albumBaseResult.setMessage("ok");
        return albumBaseResult;
    }

    /**
     * 查找所有接口
     * @return
     */
    @RequestMapping("getAlbum")
    public BaseResult<Album> save(){

        System.out.println("AlbumController===getAlbum===");
        BaseResult<Album> albumBaseResult = new BaseResult<>();

        List<Album> albume = iAlbumService.findAlbume();
        albumBaseResult.setList(albume);
        albumBaseResult.setMessage("ok");
        return albumBaseResult;
    }
}
